# -*- coding: utf-8 -*-
__author__ = 'yangq'

from appcommon.appinstance import mysql_app_pool
from tornado import gen

from common import timetool


@gen.coroutine
def get_circle_list(c_id, page_count, type, couch):
    param = "and a.id < {} ".format(c_id) if c_id else ""
    param2 = "and a.is_top=1 " if type == 'top' else ""
    param += param2
    sql = (
        "select a.id as c_id, a.phone, a.is_using, a.is_top, a.create_time, text, images,b.nick_name,b.avatar from app_circle_info a, "
        "app_user_info b where  a.phone=b.phone "
        "and a.is_using in (1,2) {} "
        "and a.couch = {} "
        "order by a.id desc limit {};"
    )
    res = yield mysql_app_pool.get_all(sql.format(param, couch, page_count), cursor='dict')
    for item in res:
        item['images'] = item['images'].split()
        item['create_time'] = timetool.convert_timestamp_to_date(item['create_time'])
    raise gen.Return(res)


@gen.coroutine
def get_reply_list(c_id, page_count, reply_id):
    param = "and a.id < {} ".format(reply_id) if reply_id else ""
    sql = (
        "select a.id as reply_id, a.phone, a.is_using,a.create_time, content, images,b.nick_name,b.avatar from app_circle_reply a, "
        "app_user_info b where  a.phone=b.phone and a.c_id={} "
        "and a.is_using in (1,2) {} "
        "order by a.id desc limit {};"
    )
    print sql.format(c_id, param, page_count)
    res = yield mysql_app_pool.get_all(sql.format(c_id, param, page_count), cursor='dict')
    for item in res:
        item['images'] = item['images'].split()
        item['create_time'] = timetool.convert_timestamp_to_date(item['create_time'])
    raise gen.Return(res)



@gen.coroutine
def get_reply_cnt(c_id):
    sql = (
        "select count(*) as cnt "
        "from app_circle_reply where 1=1 "
        u"and c_id = {} and is_using = 1"
    )
    res = yield mysql_app_pool.get_one(sql.format(
        c_id
    ), cursor='dict')
    raise gen.Return(res['cnt'])


@gen.coroutine
def get_zan_cnt(c_id):
    sql = "select count(*) as cnt from app_circle_zan where c_id = {}"
    res = yield mysql_app_pool.get_one(sql.format(
        c_id
    ), cursor='dict')
    raise gen.Return(res['cnt'])


@gen.coroutine
def check_user_silence(phone):
    sql = "select 1  from app_circle_silence_user where phone = '{}'"
    tmp = yield mysql_app_pool.get_one(sql.format(
        phone
    ), cursor='dict')
    res = 1 if tmp else 0
    raise gen.Return(res)


@gen.coroutine
def change_circle_status(c_id, is_using):
    sql = "update app_circle_info set is_using={} where id = {}"
    res = yield mysql_app_pool.update(sql.format(
        is_using, c_id
    ))
    raise gen.Return(res)


@gen.coroutine
def change_circle_top(c_id, is_top):
    sql = "update app_circle_info set is_top={} where id = {}"
    res = yield mysql_app_pool.update(sql.format(
        is_top, c_id
    ))
    raise gen.Return(res)


@gen.coroutine
def change_reply_status(reply_id, is_using):
    sql = "update app_circle_reply set is_using={} where id = {}"
    res = yield mysql_app_pool.update(sql.format(
        is_using, reply_id
    ))
    raise gen.Return(res)


@gen.coroutine
def set_silence(phone, current_time, is_silence):
    if is_silence == '1':
        sql = "insert into app_circle_silence_user (phone, create_time) values ('{}', {})"
        res = yield mysql_app_pool.insert_one(sql.format(
            phone, current_time
        ))
    else:
        sql = "delete from app_circle_silence_user where phone = '{}'"
        res = yield mysql_app_pool.delete(sql.format(
            phone
        ))
    raise gen.Return(res)
